package com.rookieci.plugin.core;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.NonNull;

/**
 * Created by 磁磁帅 on 2023/4/27 0027.
 * 功能：所有的插件类需要继承这个BaseActivity,否则无法启动
 * 使用AppCompatActivity会闪退，比较无奈
 */
public class BaseActivity extends Activity implements PluginInterface {
    //这个that是最重要的上下文，是PluginActivity的实列，这样我们就可以意识到
    //我们所有的系统级的api都是通过调用that里的方法实现的，因为我们所有的插件
    //别看都是继承与BaseActivity,但BaseActivity的方法都是调用的that,所以所有
    //的插件activity都是共享PluginActivity上下文。
    protected Activity that;

    @Override
    public void attach(Activity activity) {
        that = activity;
    }

    @Override
    public void setContentView(int layoutResID) {
        that.setContentView(layoutResID);
    }

    @Override
    public void setContentView(View view) {
        that.setContentView(view);
    }

    @Override
    public <T extends View> T findViewById(int id) {
        return that.findViewById(id);
    }

    @Override
    public Intent getIntent() {
        return that.getIntent();
    }

    @Override
    public Resources getResources() {
        return that.getResources();
    }

    @Override
    public WindowManager getWindowManager() {
        return that.getWindowManager();
    }

    @Override
    public Window getWindow() {
        return that.getWindow();
    }

    @Override
    public void startActivity(Intent intent) {
        Intent i = new Intent();
        i.putExtra("className", intent.getComponent().getClassName());
        that.startActivity(i);
    }

    @NonNull
    @Override
    public LayoutInflater getLayoutInflater() {
        return that.getLayoutInflater();
    }

    @Override
    public ClassLoader getClassLoader() {
        return that.getClassLoader();
    }

    @SuppressLint("MissingSuperCall")
    @Override
    public void onCreate(Bundle savedInstance) {
    }

    @SuppressLint("MissingSuperCall")
    @Override
    public void onStart() {
    }

    @SuppressLint("MissingSuperCall")
    @Override
    public void onResume() {

    }

    @SuppressLint("MissingSuperCall")
    @Override
    public void onPause() {

    }

    @SuppressLint("MissingSuperCall")
    @Override
    public void onStop() {
    }

    @SuppressLint("MissingSuperCall")
    @Override
    public void onDestroy() {
    }

    @SuppressLint("MissingSuperCall")
    @Override
    public void onSaveInstanceState(Bundle outState) {
    }

    //这个千万不要调用that.onBackPressed(),不然会出现应用死循环，一直调用onBackPressed()
    @Override
    public void onBackPressed() {
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return true;
    }

    @Override
    public ApplicationInfo getApplicationInfo() {
        return that.getApplicationInfo();
    }
}
